package com.itheima.web.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Role;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.impl.RoleServiceImpl;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/system/role")
public class RoleServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            //查询所有
            this.findAll(request, response);
        } else if ("toAdd".equals(operation)) {
            //跳转添加页面
            this.toAdd(request, response);
        } else if ("save".equals(operation)) {
            //添加信息
            this.save(request, response);
        } else if ("toEdit".equals(operation)) {
            //跳转编辑页面
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
            //保存修改信息
            this.edit(request, response);
        } else if ("delete".equals(operation)) {
            //删除信息
            this.delete(request, response);
        }else if ("author".equals(operation)) {
            //根据当前角色Id查询所有模块信息,并且加载关系数据
            this.author(request, response);
        }else if ("updateRoleModule".equals(operation)) {
            //建立模块与角色之间的联系
            this.updateRoleModule(request, response);
        }
    }

    private void updateRoleModule(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String roleId = request.getParameter("roleId");
        String moduleIds = request.getParameter("moduleIds");
        roleService.updateRoleModule(roleId,moduleIds);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");
    }

    private void author(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String roleId = request.getParameter("id");
        Role role = roleService.findById(roleId);
        request.setAttribute("role",role);
        //根据当前角色Id查询所有模块信息,并且加载关系数据
        List<Map> authorDataByRoleId = moduleService.findAuthorDataByRoleId(roleId);
        ObjectMapper om =new ObjectMapper();
        String json = om.writeValueAsString(authorDataByRoleId);
        request.setAttribute("roleModuleJson",json);
        request.getRequestDispatcher("/WEB-INF/pages/system/role/author.jsp").forward(request, response);

    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //RoleService roleService =new RoleServiceImpl();
        Role role = BeanUtil.fillBean(request, Role.class);
        roleService.delete(role);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");
    }

    //保存编辑信息
    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //RoleService roleService =new RoleServiceImpl();
        Role role = BeanUtil.fillBean(request, Role.class, "yyyy-MM-dd");
        roleService.update(role);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");
    }

    //跳转编辑页面
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        //RoleService roleService = new RoleServiceImpl();
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        request.getRequestDispatcher("/WEB-INF/pages/system/role/update.jsp").forward(request, response);
    }


    //添加信息
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //RoleService roleService =new RoleServiceImpl();
        Role role = BeanUtil.fillBean(request, Role.class, "yyyy-MM-dd");
        roleService.save(role);
        response.sendRedirect(request.getContextPath() + "/system/role?operation=list");


    }

    //跳转添加页面
    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Role> all = roleService.findAll();
        request.setAttribute("roleList", all);
        request.getRequestDispatcher("/WEB-INF/pages/system/role/add.jsp").forward(request, response);
    }


    //查询所有
    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //RoleService roleService =new RoleServiceImpl();
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = roleService.findAll(page, size);
        request.setAttribute("page", all);
        request.getRequestDispatcher("/WEB-INF/pages/system/role/list.jsp").forward(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
